from rest_framework import serializers
from .models import *

# 用户序列化器
class UserSer(serializers.ModelSerializer):        # 只用于序列化输入 # 去掉两边的空格
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField(read_only=True)
    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = UserModel
        fields = ["id","nickname","avatar_url","mobile","password","role","role_name","date_joined","last_login","is_staff"]
        read_only_fields = ["id","date_joined"]

# 地址序列化器
class AddressSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname

    class Meta:
        model = AddressModel
        fields = "__all__"

# 订单序列化器
class OrderSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()
    shops_avatar_url = serializers.SerializerMethodField()
    address = serializers.PrimaryKeyRelatedField(queryset=AddressModel.objects.all(),required=False,allow_null=True)
    address_name = serializers.SerializerMethodField()
    coupons = serializers.PrimaryKeyRelatedField(queryset=UserCouponsModel.objects.all(),required=False,allow_null=True)
    coupons_name = serializers.SerializerMethodField()

    pay_method_name = serializers.SerializerMethodField()
    pay_state_name = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname
    def get_shops_name(self, obj):
        return obj.shops.nickname
    def get_shops_avatar_url(self, obj):
        return obj.shops.avatar_url
    def get_address_name(self, obj):
        return obj.address.nickname
    def get_coupons_name(self, obj):
        if not obj.coupons:
            return None
        return obj.coupons.coupons.name

    def get_pay_method_name(self, obj):
        return obj.get_pay_method_display()
    def get_pay_state_name(self, obj):
        return obj.get_pay_state_display()
    class Meta:
        model = OrderModel
        fields = "__all__"

# 订单商品序列化器
class OrderGoodsSer(serializers.ModelSerializer):
    order = serializers.PrimaryKeyRelatedField(queryset=OrderModel.objects.all())
    goods = serializers.PrimaryKeyRelatedField(queryset=GoodsModel.objects.all())
    goods_name = serializers.SerializerMethodField()

    def get_goods_name(self, obj):
        return obj.goods.name

    class Meta:
        model = OrderGoodsModel
        fields = "__all__"

# 商家评论序列化器
class CommentSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()
    order = serializers.PrimaryKeyRelatedField(queryset=OrderModel.objects.all())
    def get_user_name(self, obj):
        return obj.user.nickname
    def get_shops_name(self, obj):
        return obj.shops.nickname

    class Meta:
        model = CommentModel
        fields = "__all__"

# 商家评论回复序列化器
class ReplySer(serializers.ModelSerializer):
    comment = serializers.PrimaryKeyRelatedField(queryset=CommentModel.objects.all())
    # # comment_name = serializers.SerializerMethodField()
    def get_comment_name(self,obj):
        return obj.comment.name

    class Meta:
        model = ReplyModel
        fields = '__all__'

